<?php
// include_once "./code/cx.php";
// echo "<pre>\n";
$data=file_get_contents('php://input');
$data = json_decode($data, true);
echo json_encode($data);
die;
$data = [];
$ids = [];

// delete from items where name='五常稻花香25KG' and 

/* // orders 转 items
$a = GetData(['table' => 'orders'], 'itemInfo');
foreach ($a as $b) {
  $v = json_decode($b['itemInfo'], true);
  if (!in_array($v['ID'], $ids)) {
    $ids[] = $v['ID'];
    $tmp = 'update items set useTime=0,name=\'' . $v['name'] . '\',CNY=' . (int) $v['CNY'] . ',imgs=\'' . $v['imgs'] . '\',sum='.$v['sum'].',info=\''.$v['info'].'\',power='.$v['power'].' where ID=' . $v['ID'] . ';';
    $data[] = $tmp;
    runSql($tmp);
  }
}
*/
/* // transactionlog 转 items
$a = GetData(['table' => 'transactionlog'], 'items');
foreach ($a as $b) {
  $b = json_decode($b['items'], true);
  foreach ($b as $v) {
    if (!in_array($v['ID'], $ids)) {
      $ids[] = $v['ID'];
      if (substr($v['img'], 0, 2) == ' h') $v['img'] = substr($v['img'], 24);
      $tmp = 'update items set name=\'' . $v['name'] . '\',CNY=' . (int) ($v['price'] * 100) . ',imgs=\'' . json_encode([['url' => $v['img']]]) . '\' where ID=' . $v['ID'] . ';';
      $data[] = $tmp;
      runSql($tmp);
    }
  }
}*/

/* // transactionLog 转 orders
$a = GetData(['table' => 'transactionlog']);
foreach ($a as $b) {
  $b['items'] = json_decode($b['items'], true);
  $user = GetData(['table' => 'users', 'ID' => $b['buyID']], 'name,tel,recv', true);
  if ($user) $user['recv'] = json_decode($user['recv'], true)[0]['address'];

  foreach ($b['items'] as $c) {
    $item = GetData(['table' => 'items', 'name' => $c['name']], '*', true);
    $tmp = [
      'table' => 'orders',
      'createTime' => strtotime($b['createTime']),
      'changeTime' => strtotime($b['changeTime']),
      'cuid' => 0,
      'state' => $b['state'],
      'itemID' => $item['ID'],
      'sellID' => 0,
      'buyID' => $b['buyID'],
      'sum' => $c['sum'],
      'money' => json_encode([
        'sum' => ['CNY' => $item['CNY'] * $c['sum'], 'point' => $item['point'] * $c['sum']],
        'payment' => ['CNY' => $b['money'] * 100, 'point' => 0]
      ]),
      'typeCid' => $item['typeCid'],
      'name' => $user ? $user['name'] : '测试人员',
      'tel' => $user ? $user['tel'] : '13300002222',
      'address' => $user ? $user['recv'] : '园中园小区 测试门牌号',
      'itemInfo' => json_encode($item, JSON_UNESCAPED_UNICODE),
      'other' => '',
      'tradeNo' => $b['tradeNo'],
    ];
    AddData($tmp);
    // mPrintX($tmp);
  }
} */

/* // 更新订单 items
$a = GetData(['table' => 'orders'], 'ID,itemID');
foreach ($a as $b) {
  $ids = GetData(['table' => 'items', 'ID' => $b['itemID']], '*', true);
  $ids = "update `orders` set itemInfo='" . formatMysql(json_encode($ids, JSON_UNESCAPED_UNICODE)) . "' where ID=" . $b['ID'];
  runSql($ids);
} */
// $a = GetData(['table' => 'orders', 'ID' => 1], 'ID,itemInfo', true);
// $aa = $a['itemInfo'];
// $b = GetData(['table' => 'items', 'ID' => 28], '', true);
// $bb = json_encode($b, JSON_UNESCAPED_UNICODE);
// print_r(json_decode($aa,true));
// echo"\n\n\n";
// echo '['.substr($aa,129,2).']'; 
// echo"\n\n\n";
// echo '['.substr($bb,129,2).']'; 
// echo "\n\n\n";
// print_r($bb);
